package com.no1.musicmgr.service;

import com.no1.musicmgr.util.CodeUtil;
import com.no1.musicmgr.util.ImgCodeUtil;
import com.no1.musicmgr.vo.Imgcode;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Log4j
@Service
public class CodeImgService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    public String CodeFromRedis01(){
        // 获取验证码 4为常规的验证码
        String code = CodeUtil.getCode01(4);
        //创建一个图片
        String path = "c:\\static\\directory\\output.png";
        ImgCodeUtil.convertStringToImage(code, path);
        // 将验证码存入 Redis
        String key = "code_key";
        redisTemplate.opsForValue().set(key, code);
        // 从 Redis 中获取验证码
        code = redisTemplate.opsForValue().get(key);
        return code;
    }
    public Imgcode CodeFromRedis02() {
        // 获取验证码 4为常规的验证码
        Imgcode img = CodeUtil.getCode02(20);
        String code = img.getCode();
        Integer res = img.getRes();
        //创建一个图片
        String path = "c:\\static\\directory\\output.png";
        ImgCodeUtil.convertStringToImage(code, path);
        // 将验证码存入 Redis
        String key = "code_key";
        redisTemplate.opsForValue().set(key, String.valueOf(res));
        // 从 Redis 中获取验证码
        res = Integer.valueOf(redisTemplate.opsForValue().get(key));
        img.setRes(res);
        return img;
    }

}
